﻿using MediatR;
using RBAC.Domain;
using RBAC.Domain.Dto;
using Sowrk03_28.Repository;
using Swork03_28.Api.Application.Commands.UserCommand;
using Swork03_28.Domain.Dto;

namespace Swork03_28.Api.Application.CommandHandlers.UserCommandHandler
{
    public class UserCommandHandler : IRequestHandler<UserCommand, LoginDto>
    {
        private readonly IRepository<UserInfo> _userInfoRepository;

        public UserCommandHandler(IRepository<UserInfo> userInfoRepository)
        {
            _userInfoRepository = userInfoRepository;
        }

        public async Task<LoginDto> Handle(UserCommand request, CancellationToken cancellationToken)
        {
            var info = await _userInfoRepository.GetValues(x => x.Name == request.UserName && x.Pwd == request.Pwd);
            var user = info.FirstOrDefault();
            if (user != null)
            {
                return new LoginDto
                {
                    Token = null,
                    UserName = user.Name,
                    UserId = user.Id,
                };
            }
            else
            {
                return null;
            }
        }
    }
}
